<template>
	<view>
		<u-cell-group :border="false">
			<view style="background: #fff;margin-top: 20rpx;">
				<u-cell title="头像" isLink :border="false" @click="itemClick(1, '', formData.avatar||'')">
					<template #value>
						<u-avatar :src="formData.avatar||''" size="40" mode="aspectFill"></u-avatar>
					</template>
				</u-cell>
			</view>
			<view style="background: #fff;margin-top: 20rpx;">
				<u-cell title="昵称" isLink :border="true" :value="formData.name||'--'" @click="itemClick(2,'name',formData.name||'')"></u-cell>
				<u-cell title="性别" isLink :border="true" :value="dictLabel(sex, formData.sex)" @click="itemClick(3,'',formData.sex)"></u-cell>
				<u-cell title="手机号码" :border="true" :value="formData.phone||userphone||'--'"></u-cell>
				<u-cell title="政治面貌" isLink :border="true" :value="dictLabel(political, formData.politicalStatus)" @click="itemClick(4,'',formData.politicalStatus)"></u-cell>
				<u-cell title="文化程度" isLink :border="false" :value="dictLabel(eduLevel, formData.education)" @click="itemClick(5,'',formData.education)"></u-cell>
			</view>
			<!-- <view style="background: #fff;margin-top: 20rpx;">
				<u-cell title="证书编号" :border="true" :value="formData.certNo || '暂无'"></u-cell>
				<u-cell title="工作单位" isLink :border="true" :value="formData.workCorp||'--'" @click="itemClick(6,'workCorp',formData.workCorp||'')"></u-cell>
				<u-cell title="所在班组" isLink :border="true" :value="formData.dept||'--'" @click="itemClick(7,'dept',formData.dept||'')"></u-cell>
				<u-cell title="常住地址" isLink :border="false" :value="formData.address||'--'" @click="itemClick(8,'address',formData.address||'')"></u-cell>
			</view> -->
		</u-cell-group>
		
		<sy-picker ref="sypicker" mobile="1" :type="0" :items="selectData" @select="handleSelect" />
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad, onShow } from "@dcloudio/uni-app";
import {sex, eduLevel, political, dictLabel} from "@/utils/common.js"
import { personInfoList,personInfoEdit } from '@/api/qss/zxhapi.js'

const userphone = uni.getStorageSync('phone')||''
const formData = ref({})
const selectData = ref([])
const optionData = ref({
	option_3: sex,
	option_4: political,
	option_5: eduLevel
})
const sypicker = ref('')
const active = ref(0)

function itemClick(type,name, val){
	if(type==1){
		// uni.chooseImage({
		// 	count: 1,
		// 	success: res => {
		// 		if (res.tempFiles[0].size < 10*1024*1024) {
		// 			let file = { path: res.tempFilePaths[0], name: '', type: "file", }
		// 			console.log(file)
					
		// 			formData.value.avatar = res.tempFilePaths[0]
		// 		} else {
		// 			uni.showModal({ title: '', showCancel: false, content: '超出10M限制大小' })
		// 		}
		// 	}
		// })
		if(val){
			uni.previewImage({
				current: 0, urls: [val]
			})
		}
	}else if(type==2||type==6||type==7||type==8){
		uni.setStorageSync('qssuseredit', formData.value)
		uni.navigateTo({
			url:'/pagesC/mine/editsave?type='+type+'&name='+name+'&val='+val
		})
	}else if(type==3||type==4||type==5){
		active.value = type
		selectData.value = optionData.value['option_'+type]
		sypicker.value.showPicker(val)
	}
}

function handleSelect(e){
	let param = JSON.parse(JSON.stringify(formData.value))
	if(active.value==3) {
		param.sex = e.value
	} else if(active.value==4) {
		param.politicalStatus = e.value
	} else if(active.value==5) {
		param.education = e.value
	}
	if ( RegExp('platform=corp').test(param.avatar) ) {
		param.avatar = param.avatar.split('?platform=corp')[0]
	}
	personInfoEdit(param).then(res=>{
		if(res.code == 200){
			uni.showToast({ title: "提交成功", duration: 1000 })
			if(active.value==3) {
				formData.value.sex = e.value
			} else if(active.value==4) {
				formData.value.politicalStatus = e.value
			} else if(active.value==5) {
				formData.value.education = e.value
			}
		}
	}).finally(()=>{
		uni.hideLoading()
	})
}

function getCont(){
	personInfoList({phone: userphone}).then(res=>{
		console.log(res)
		if(res.rows?.length){
			let token = uni.getStorageSync('token')
			formData.value = res.rows[0]
			formData.value.avatar = formData.value.avatar?formData.value.avatar+`?platform=corp&Authorization=Bearer ${token}`:''
		}
	}).finally(()=>{
		uni.hideLoading()
	})
}

onLoad((option)=>{
	formData.value = uni.getStorageSync('userInfo')||{}
})

onShow(()=>{
	if(userphone) {
		getCont()
	}
})

</script>

<style lang="less">
page{
	background: #f8f8f8;
}
</style>